﻿using System;

namespace SequenceOfMaximalSum
{
    class SequenceOfMaximalSum
    {
        static void PrintResult(int[] result)
        {
            Console.Write("{ ");
            for (int index = 0, length = result.Length - 1; index < length; index++)
            {
                Console.Write("{0}, ", result[index]);
            }
            Console.WriteLine("{0} }}", result[result.Length - 1]);
        }
        static int[] MaximalSequenceSum(int[] numbers)
        {
            int[] result;
            int resultLength;
            long maxSum = long.MinValue;
            int startIndex = 0,
                endIndex = 0;

            for (int index = 0, length = numbers.Length; index < length; index++)
            {
                long tmpSum = numbers[index];
                for (int innerIndex = index + 1; innerIndex < length; innerIndex++)
                {
                    tmpSum += numbers[innerIndex];
                    if (tmpSum > maxSum)
                    {
                        maxSum = tmpSum;
                        startIndex = index;
                        endIndex = innerIndex;
                    }
                }
            }

            resultLength = (endIndex - startIndex) + 1;
            result = new int[resultLength];
            Array.Copy(numbers, startIndex, result, 0, resultLength);
            return result;
        }
        static void Main(string[] args)
        {
            int arrayLength;
            int[] numbers,
                result;

            Console.Write("Please enter the length of your array: ");
            arrayLength = int.Parse(Console.ReadLine());
            numbers = new int[arrayLength];
            for (int index = 0; index < arrayLength; index++)
            {
                Console.Write("Index [{0}]: ", index);
                numbers[index] = int.Parse(Console.ReadLine());
            }
            result = MaximalSequenceSum(numbers);
            PrintResult(result);
        }
    }
}
